//===- UniqueGCFactoryBaseTest.cpp ----------------------------------------===//
//
//                     The MCLinker Project
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
#include <mcld/MC/ContextFactory.h>
#include <mcld/Support/MemoryAreaFactory.h>
#include <mcld/Support/MsgHandling.h>
#include <mcld/Support/TargetSelect.h>
#include <mcld/Support/Path.h>
#include "UniqueGCFactoryBaseTest.h"

using namespace mcld;
using namespace mcldtest;


// Constructor can do set-up work for all test here.
UniqueGCFactoryBaseTest::UniqueGCFactoryBaseTest()
{
  InitializeAllDiagnostics();

  m_pLDInfo = new MCLDInfo("arm-none-linux-gnueabi", 10, 10);
  m_pLineInfo = new DiagnosticLineInfo();
  mcld::InitializeDiagnosticEngine(*m_pLDInfo, m_pLineInfo, NULL);
}

// Destructor can do clean-up work that doesn't throw exceptions here.
UniqueGCFactoryBaseTest::~UniqueGCFactoryBaseTest()
{
  delete m_pLDInfo;
  delete m_pLineInfo;
}

// SetUp() will be called immediately before each test.
void UniqueGCFactoryBaseTest::SetUp()
{
}

// TearDown() will be called immediately after each test.
void UniqueGCFactoryBaseTest::TearDown()
{
}

//==========================================================================//
// Testcases
//
TEST_F( UniqueGCFactoryBaseTest, number_constructor ) {
	ContextFactory *contextFactory = new ContextFactory(10); 
	contextFactory->produce("/");
	contextFactory->produce("ab/c");
	ASSERT_EQ( 2, contextFactory->size());
	delete contextFactory;
}

TEST_F( UniqueGCFactoryBaseTest, unique_produce ) {
	ContextFactory *contextFactory = new ContextFactory(10); 
	LDContext* context1 = contextFactory->produce("/");
	contextFactory->produce("ab/c");
	ASSERT_EQ( 2, contextFactory->size());
	LDContext* context2 = contextFactory->produce("/");
	ASSERT_EQ( context1, context2 );
	delete contextFactory;
}

TEST_F( UniqueGCFactoryBaseTest, unique_produce2 ) {
	ContextFactory *contextFactory = new ContextFactory(10); 
	LDContext* context1 = contextFactory->produce("abc/def");
	contextFactory->produce("ab/c");
	ASSERT_EQ( 2, contextFactory->size());
	LDContext* context2 = contextFactory->produce("ttt/../abc/def");
	ASSERT_EQ( context1, context2 );
	delete contextFactory;
}

TEST_F( UniqueGCFactoryBaseTest, iterator )
{
        sys::fs::Path path1(TOPDIR), path2(TOPDIR);
	path1.append("unittests/test1.txt");
	path2.append("unittests/test2.txt");

	MemoryAreaFactory* memFactory = new MemoryAreaFactory(10);
	MemoryArea* area1 = memFactory->produce(path1, FileHandle::ReadOnly);
	MemoryArea* area2 = memFactory->produce(path2, FileHandle::ReadOnly);
	ASSERT_NE( area1, area2);

	MemoryArea* area3 = memFactory->produce(path1, FileHandle::ReadOnly);
	
	ASSERT_EQ(area1, area3);
	ASSERT_FALSE( memFactory->empty());
	ASSERT_EQ( 2, memFactory->size());
	MemoryAreaFactory::iterator aIter = memFactory->begin();
	ASSERT_EQ( area1, &(*aIter));
	++aIter;
	ASSERT_EQ( area2, &(*aIter));
	++aIter;
	MemoryAreaFactory::iterator aEnd = memFactory->end();
	ASSERT_TRUE( aEnd == aIter);
	delete memFactory;
}

